﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Dapper;
using Just.Library.Util;
using MySql.Data.MySqlClient;
using Xigua.MysqlExtensions;

namespace ConsoleApp.Util
{
    public abstract class DapperHelperBase
    {
        protected string ConnectionString { get; set; }
        public CommandType CommandType { get; set; } = CommandType.Text;
        public bool Buffered { get; set; }
        protected abstract IDbConnection OpenConnection();

        /// <summary>
        /// 查询列表数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public List<T> Query<T>(string sql, int commondTimeOut=180,object parameters = null)
        {
            using (IDbConnection conn = OpenConnection())
            {
                return conn.Query<T>(sql, parameters, null, Buffered, commondTimeOut, CommandType).ToList();
            }
        }

        /// <summary>
        /// 查询第一行的数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T QueryFirstOrDefault<T>(string sql, int CommandTimeout = 180, object parameters = null)
        {
            using (IDbConnection conn = OpenConnection())
            {
                return conn.QueryFirstOrDefault<T>(sql, parameters, null, CommandTimeout, CommandType);
            }
        }

        /// <summary>
        /// 查询第一行第一列的数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T ExecuteScalar<T>(string sql, int CommandTimeout=180,object parameters = null)
        {
            using (IDbConnection conn = OpenConnection())
            {
                return conn.ExecuteScalar<T>(sql, parameters, null, CommandTimeout, CommandType);
            }
        }

        /// <summary>
        /// 执行增，删，改 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql">sql</param>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Execute(string sql, int CommandTimeout = 180, object parameters = null)
        {
            using (IDbConnection conn = OpenConnection())
            {
                return conn.Execute(sql, parameters, null, CommandTimeout, CommandType);
            }
        }

        /// <summary>
        /// 查询DataSet
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public DataSet QueryDataSet(string sql, object parameters = null)
        {
            DataSet ds = new DataSet();
            using (IDbConnection conn = OpenConnection())
            {
                using (var reader = conn.ExecuteReader(sql, parameters, commandType: CommandType))
                {
                    while (!reader.IsClosed)
                    {
                        ds.Tables.Add().Load(reader);
                    }
                }
            }
            return ds;
        }
	}
}
